package com.glory.brepda.activity.tool;

import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.activity.mfg.BomInfoQueryActivity;
import com.glory.brepda.adapter.BomInfoListAdapter;
import com.glory.brepda.adapter.ToolBomInfoListAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.BomEntity;
import com.glory.brepda.entity.BomInfoEntity;
import com.glory.brepda.entity.ToolBomLineEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import butterknife.BindView;

public class ToolBomDeleteActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.etLotId)
    FreeUI_ClearEditText etLotId;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.rvToolBomList)
    RecyclerView rvToolBomList;
    @BindView(R.id.tePartName)
    TextView tePartName;

    private List<ToolBomLineEntity.ResultBean> toolBomInfoList = new ArrayList<>();
    private ToolBomInfoListAdapter myAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_tool_bom_info_delete;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        tvTitle.setText("治具BOM删除");
        tvBaseSubmit.setText("查询");
        initRecyclerView(rvToolBomList,this);
        myAdapter = new ToolBomInfoListAdapter(null,this);
        myAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        myAdapter.isFirstOnly(true);
        myAdapter.setOnItemClickListener((baseQuickAdapter, view, i) -> {
            ToolBomLineEntity.ResultBean item = (ToolBomLineEntity.ResultBean)baseQuickAdapter.getItem(i);

            // 触发了点击事件
//            delete(String.valueOf(item.getRESERVED1()),String.valueOf(item.getTOOLSPEC()));
            delete(String.valueOf(item.getRESERVED1()),String.valueOf(item.getTOOLSPEC()));
        });
        rvToolBomList.setAdapter(myAdapter);

    }


    @Override
    protected void initData() {
        initEdit(etLotId, this::submit);
    }

    private void submit(String etLotId) {
        toolBomInfoList.clear();
        FreeUI_EntityCallBack<BaseResponseBean<ToolBomLineEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<ToolBomLineEntity>>
                (new TypeToken<BaseResponseBean<ToolBomLineEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<ToolBomLineEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    ToolBomLineEntity body = response.body().getResponse().getBody();
                    for (ToolBomLineEntity.ResultBean entity : body.getTOOLBOMLINELIST()){
                        if(!tePartName.getText().equals(entity.getRESERVED1())){
                            tePartName.setText(entity.getRESERVED1());
                        }
                        toolBomInfoList.add(entity);
                    }
                    //实现去重
                    Set<ToolBomLineEntity.ResultBean> set = new HashSet<>(toolBomInfoList);
                    List<ToolBomLineEntity.ResultBean> finalToolBomList = new ArrayList<>(set);
//                    listSort(bomInfoList);
                    myAdapter.setNewData(finalToolBomList);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<ToolBomLineEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "BOMINFOQUERY");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("LOTID",etLotId);
        map.addProperty("ACTIONTYPE","QUERYTOOLBOM");
        NetUtils.requestPostNet("IF.TOOLBOMBASIC","Common", ToolBomDeleteActivity.this,map,callBack);
    }


    private void delete(String partName,String toolSpec){


        FreeUI_EntityCallBack<BaseResponseBean<ToolBomLineEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<ToolBomLineEntity>>
                (new TypeToken<BaseResponseBean<ToolBomLineEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<ToolBomLineEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    ToolBomLineEntity body = response.body().getResponse().getBody();
                    showToast("删除成功",true);
                }
                submit(etLotId.getText().toString());
            }

            @Override
            public void onError(Response<BaseResponseBean<ToolBomLineEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "BOMINFOQUERY");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("PARTNAME",partName);
        map.addProperty("TOOLSPEC",toolSpec);
        map.addProperty("ACTIONTYPE","DELETEBOM");
        NetUtils.requestPostNet("IF.TOOLBOMBASIC","Common", ToolBomDeleteActivity.this,map,callBack);

    }
}
